package com.elitel.frame.base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elitel.frame.base.service.SysValidationService;
import com.elitel.frame.main.entity.SysValidation;

/**
 * @Description 校验规则维护
 * @author guoyanfei
 * @date 2018/01/05
 */
@Controller
@RequestMapping("/system/sysvalidation")
public class SystemValidationController {
	private static final Logger logger = LoggerFactory.getLogger(TableEditController.class);
	@Autowired
	private SysValidationService sysValidationService;
	
	/**
	 * @Description 查询所有校验规则
	 * @return SysValidation实体List集合
	 * @author guoyanfei
	 * @date 2018/01/05
	 */
	@RequestMapping("/getAllValidation")
	@ResponseBody
	public List<SysValidation> getAllValidation(HttpServletRequest request){
		List<SysValidation> sysValidations = new ArrayList<SysValidation>();
		try {
			String seachTxt = request.getParameter("seachTxt");
			sysValidations = sysValidationService.selectAllValidation(seachTxt);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return sysValidations;
	}
	
	/**
	 * @Description 新增校验规则
	 * @param sysValidation实体
	 * @return 新增结果
	 * @author guoyanfei
	 * @date 2018/01/05
	 */
	@RequestMapping("/addValidation")
	@ResponseBody
	public Integer addValidation(@RequestBody SysValidation sysValidation){
		Integer result = 0;
		try {
			result = sysValidationService.insertValidation(sysValidation);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	/**
	 * @Description 修改校验规则
	 * @param sysValidation提示
	 * @return 修改结果
	 * @author guoyanfei
	 * @date 2018/01/05
	 */
	@RequestMapping("/updateValidation")
	@ResponseBody
	public Integer updateValidation(@RequestBody SysValidation sysValidation){
		Integer result = 0;
		try {
			result = sysValidationService.updateValidation(sysValidation);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	/**
	 * @Description 批量删除校验规则
	 * @param request
	 * @return
	 */
	@RequestMapping("/delValidation")
	@ResponseBody
	public Integer delValidation(HttpServletRequest request){
		Integer result = 0;
		try {
			String guids = request.getParameter("guids");
			result = sysValidationService.deleteValidation(guids);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	/**
	 * @Description 查询校验Code是否存在
	 * @param validateCode(校验Code)
	 * @return true(存在)/false(不存在)
	 * @author guoyanfei
	 * @date 2018/01/05
	 */
	@RequestMapping("/isExitValidateCode")
	@ResponseBody
	public boolean isExitValidateCode(HttpServletRequest request){
		boolean result = false;
		try {
			String validateCode = request.getParameter("validateCode");
			SysValidation sysValidation = sysValidationService.selectValidationBycode(validateCode);
			if(sysValidation != null){
				result = true;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return result;
	}
	
	/**
	 * @Description 通过校验规则Code查询校验规则信息
	 * @param validatecode(校验规则Code)
	 * @return SysValidation实体
	 * @author guoyanfei
	 * @date 2018/01/06
	 */
	@RequestMapping("/getValidationByCode")
	@ResponseBody
	public SysValidation getValidationByCode(HttpServletRequest request){
		SysValidation sysValidation = new SysValidation();
		try {
			String validatecode = request.getParameter("validatecode");
			sysValidation = sysValidationService.selectValidationBycode(validatecode);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		
		return sysValidation;
	}

}
